package com.alks.function.controller.pcFactoryQuery;

import com.alks.common.aop.ApiRestController;
import com.alks.common.aop.RoleCheck;
import com.alks.common.aop.WebLogger;
import com.alks.common.utils.DateUtils;
import com.alks.common.utils.beanutils.ArrayUtils;
import com.alks.common.utils.excelUtils.ExcelUtils;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.entity.exception.ServiceErrorException;
import com.alks.function.data.dto.pcfactoryquery.PcMtlByLotDetailDto;
import com.alks.function.data.dto.pcfactoryquery.PcMtlByLotHeadDto;
import com.alks.function.data.request.factoryquery.SpMtlDetailRequest;
import com.alks.function.data.request.factoryquery.SpMtlHeadRequest;
import com.alks.function.data.request.factoryquery.SpUpProcessRequest;
import com.alks.function.service.pcfactoryquery.IPcMtlSupportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@ApiRestController("/pcFactorQuery")
@Api(value = "总经办", tags = "总经办-工厂查询模块-材料配套查询")
@Slf4j
@RoleCheck(prgIds = {"100410","140205","150206","160205","170508","110305","130305","180708","190205"})
public class MtlMatchController {
    @Autowired
    private IPcMtlSupportService supportService;

    @PostMapping("/queryMtlHead")
    @ApiOperation(value = "材料配套-指令主料-分页条件查询",tags = "总经办-工厂查询模块-材料配套查询")
    @WebLogger
    public ResponseInfo queryMtlHead(@RequestBody SpMtlHeadRequest request){
        Map<String, Object> map = supportService.queryMtlHead(request);
        return ResponseInfo.ok(map);
    }

    @PutMapping("/queryMtlDetail")
    @ApiOperation(value = "材料配套-材料明细-分页条件查询",tags = "总经办-工厂查询模块-材料配套查询")
    @WebLogger
    public ResponseInfo queryMtlDetail(@RequestBody SpMtlDetailRequest request){
        Map<String, Object> map = supportService.queryMtlDetail(request);
        return ResponseInfo.ok(map);
    }

    @GetMapping("/queryMtlAttribute/{lotNo}")
    @ApiOperation(value = "材料配套-材料属性-条件查询",tags = "总经办-工厂查询模块-材料配套查询")
    @WebLogger
    public ResponseInfo queryMtlAttribute(@PathVariable String lotNo){
        return supportService.queryMtlAttribute(lotNo);
    }

    @PutMapping("/updateProcess")
    @ApiOperation(value = "材料配套-材料工艺-数据更新",tags = "总经办-工厂查询模块-材料配套查询")
    @WebLogger
    public ResponseInfo updateProcess(@RequestBody SpUpProcessRequest request){
        return supportService.updateProcess(request);
    }

    @PutMapping("/queryVendor")
    @ApiOperation(value = "材料配套-厂商-回表查询",tags = "总经办-工厂查询模块-材料配套查询")
    @WebLogger
    public ResponseInfo queryVendor(){
        return supportService.queryVendor();
    }

    @PutMapping("/supp/mtlHead/export")
    @ApiOperation(value = "主表导出",tags = "总经办-工厂查询模块-材料配套查询")
    @WebLogger
    public void headExport(@RequestBody SpMtlHeadRequest request, HttpServletResponse response){
        String templetName = "指令材料配套明细模版.xlsx";
        String excelName = "指令材料配套明细"+ DateUtils.dateTimeNow();
        request.setPageSize(-1);
        request.setPageNum(-1);
        Map<String, Object> map = supportService.queryMtlHead(request);
        if (ArrayUtils.isEmpyt((List<PcMtlByLotHeadDto>) map.get("mtlhead"))){
            throw new ServiceErrorException("暂无数据，无法导出");
        }
        try {
            ExcelUtils.creatByTemplate(response,templetName,excelName, (List<PcMtlByLotHeadDto>) map.get("mtlhead"),map);
        } catch (IOException e) {
            throw new ServiceErrorException("导出失败");
        }
    }

    @PutMapping("/supp/mtlDet/export")
    @ApiOperation(value = "明细导出",tags = "总经办-工厂查询模块-材料配套查询")
    @WebLogger
    public void detailExport(@RequestBody SpMtlDetailRequest request,HttpServletResponse response){
        String templetName = "材料欠料明细模版.xlsx";
        String excelName = "材料欠料明细"+ DateUtils.dateTimeNow();
        request.setPageSize(-1);
        request.setPageNum(-1);
        Map<String, Object> map = supportService.queryMtlDetail(request);
        try {
            ExcelUtils.creatByTemplate(response,templetName,excelName, (List<PcMtlByLotDetailDto>) map.get("detail"),map);
        } catch (IOException e) {
            throw new ServiceErrorException("导出失败");
        }
    }
}
